package org.feng.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.feng.api.PageBean;
import org.feng.api.ResultVo;
import org.feng.api.animal.request.AddAndUpdateAnimalRequest;
import org.feng.api.animal.request.DeleteAnimalRequest;
import org.feng.api.animal.request.QueryAnimalRequest;
import org.feng.api.animal.response.AddAndUpdateAnimalDto;
import org.feng.api.animal.response.DeleteAnimalDto;
import org.feng.api.animal.response.QueryAnimalVo;
import org.feng.api.animal.service.AnimalApi;
import org.feng.manage.animal.AnimalAddManage;
import org.feng.manage.sys.OperateContext;
import org.feng.manage.sys.OperateEnum;
import org.feng.manage.sys.OperateHandlerFactory;
import org.feng.service.IAnimalService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 动物Api接口控制器
 *
 * @author feng
 */
@RequestMapping("/api/animal")
@RestController
@Api(tags = "动物Api接口控制器")
public class AnimalController implements AnimalApi {

    @Resource
    private IAnimalService animalService;

    @Override
    @PostMapping(value = {"/delete"}, produces = {"application/json"})
    @ApiOperation("删除动物")
    public ResultVo<DeleteAnimalDto> deleteAnimal(@Valid @RequestBody DeleteAnimalRequest request) {
        return new ResultVo<>(0, null, animalService.delete(request));
    }

    @Override
    @PostMapping(value = {"/update"}, produces = {"application/json"})
    @ApiOperation("修改动物")
    public ResultVo<AddAndUpdateAnimalDto> updateAnimal(@Valid @RequestBody AddAndUpdateAnimalRequest request) {
        return new ResultVo<>(0, null, animalService.saveOrUpdate(request));
    }

    @Override
    @PostMapping(value = {"/add"}, produces = {"application/json"})
    @ApiOperation("新增动物")
    public ResultVo<AddAndUpdateAnimalDto> addAnimal(@Valid @RequestBody AddAndUpdateAnimalRequest request) {
        OperateContext<AddAndUpdateAnimalRequest> context = new OperateContext<>();
        context.setData(request);
        context.setOperate(OperateEnum.ANIMAL_ADD);
        return new ResultVo<>(0, null, context.execute());
    }

    @Override
    @PostMapping(value = {"/query"}, produces = {"application/json"})
    @ApiOperation("查询动物")
    public ResultVo<PageBean<QueryAnimalVo>> queryAnimal(@Valid @RequestBody QueryAnimalRequest request) {
        return new ResultVo<>(0, null, animalService.queryPages(request));
    }
}
